package com.csun.cmny.provider.config;

import com.csun.cmny.provider.device.csun.camera.entity.CsunCameraData;
import com.csun.cmny.provider.device.csun.gateway.entity.CsunGatewayData;
import com.csun.cmny.provider.device.csun.sleep.entity.CsunSleepData;
import com.csun.cmny.provider.device.phone.entity.PhoneDataEntity;
import com.csun.cmny.provider.service.DmcCsunCameraLocationService;
import com.csun.cmny.provider.service.DmcCsunSleepDataService;
import com.csun.cmny.provider.service.DmcGatewayDataService;
import com.csun.cmny.provider.service.DmcPhoneDataService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-04-28 19:04
 * @Description:
 */
@Component
@Order(value = 1)
@Slf4j
public class RedisInitRunner implements CommandLineRunner {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private DmcCsunSleepDataService dmcCsunSleepDataService;

    @Resource
    private DmcPhoneDataService dmcPhoneDataService;

    @Resource
    private DmcGatewayDataService dmcGatewayDataService;
    @Resource
    private DmcCsunCameraLocationService dmcCsunCameraLocationService;

    /**
     * Run.
     *
     * @param args the args
     */
    @Override
    public void run(String... args) {

        log.info(">>>>>>>>>>>>>>>服务启动执行，执行加载数据等操作<<<<<<<<<<<<<");

        log.info(">>>>>>>>>>>>>>>>>>>>>加载日创床垫数据开始<<<<<<<<<<<<<<<<<<<");
        loadCsunSleepData();
        log.info(">>>>>>>>>>>>>>>>>>>>>加载日创床垫数据结束<<<<<<<<<<<<<<<<<<<");

        log.info(">>>>>>>>>>>>>>>>>>>>>加载智能电话机数据开始<<<<<<<<<<<<<<<<<");
        loadPhoneData();
        log.info(">>>>>>>>>>>>>>>>>>>>>加载智能电话机数据结束<<<<<<<<<<<<<<<<<");

        log.info(">>>>>>>>>>>>>>>>>>>>>加载智能网关数据开始<<<<<<<<<<<<<<<<<<<");
        loadGatewayData();
        log.info(">>>>>>>>>>>>>>>>>>>>>加载智能网关数据结束<<<<<<<<<<<<<<<<<<<");

        log.info(">>>>>>>>>>>>>>>>>>>>>加载孝心手环数据开始<<<<<<<<<<<<<<<<<<<");
        loadXxWatchData();
        log.info(">>>>>>>>>>>>>>>>>>>>>加载孝心手环数据结束<<<<<<<<<<<<<<<<<<<");

        log.info(">>>>>>>>>>>>>>>>>>>>>加载智能摄像头数据开始<<<<<<<<<<<<<<<<<<<");
        loadCsunCameraData();
        log.info(">>>>>>>>>>>>>>>>>>>>>加载智能摄像头数据结束<<<<<<<<<<<<<<<<<<<");
    }

    public void loadXxWatchData() {

        // 查询
    }

    public void loadCsunCameraData() {

        List<CsunCameraData> csunCameraDataList = dmcCsunCameraLocationService.queryCsunCameraBindList();
        if (PublicUtil.isNotEmpty(csunCameraDataList) && csunCameraDataList.size() > 0) {
            for (CsunCameraData data : csunCameraDataList) {
                if (PublicUtil.isEmpty(data.getDeviceNo())) {
                    continue;
                }
                String redisKey = RedisKeyUtil.getCsunCameraKey(data.getDeviceNo());
//                CsunCameraData csunCameraData = (CsunCameraData) redisTemplate.opsForValue().get(redisKey);
//                if (PublicUtil.isEmpty(csunCameraData)) {
//                    continue;
//                }
//                data.setStatus(DeviceStatusEnum.OFFLINE.getKey());
                redisTemplate.opsForValue().set(redisKey, data);
            }
        }
    }

    /**
     * 加载日创床垫数据
     */
    public void loadCsunSleepData() {
        // 查询所有绑定了的日创床垫
        List<CsunSleepData> csunSleepDataList = dmcCsunSleepDataService.queryCsunSleepDataBindList();
        if (PublicUtil.isNotEmpty(csunSleepDataList) && csunSleepDataList.size() > 0) {
            for (CsunSleepData data : csunSleepDataList) {
                if (PublicUtil.isEmpty(data.getDeviceNo())) {
                    continue;
                }
                String redisKey = RedisKeyUtil.getCsunSleepKey(data.getDeviceNo());
                CsunSleepData csunSleepData = (CsunSleepData) redisTemplate.opsForValue().get(redisKey);
                if (PublicUtil.isEmpty(csunSleepData)) {
                    continue;
                }
                redisTemplate.opsForValue().set(redisKey, data);
            }
        }
    }

    /**
     * 加载智能电话机数据
     */
    public void loadPhoneData() {
        // 查询所有绑定了的智能电话机
        List<PhoneDataEntity> phoneDataList = dmcPhoneDataService.queryPhoneDataBindList();
        if (PublicUtil.isNotEmpty(phoneDataList) && phoneDataList.size() > 0) {
            for (PhoneDataEntity data : phoneDataList) {
                if (PublicUtil.isEmpty(data.getDeviceNo())) {
                    continue;
                }
                String redisKey = RedisKeyUtil.getZnPhoneKey(data.getDeviceNo());
                PhoneDataEntity phoneDataEntity = (PhoneDataEntity) redisTemplate.opsForValue().get(redisKey);
                if (PublicUtil.isNotEmpty(phoneDataEntity)) {
                    continue;
                }
                redisTemplate.opsForValue().set(redisKey, data);
            }
        }
    }

    /**
     * 加载智能网关数据
     */
    public void loadGatewayData() {
        // 查询所有的智能网关
        List<CsunGatewayData> gatewayDataList = dmcGatewayDataService.queryGateWayDataList();
        if (PublicUtil.isNotEmpty(gatewayDataList) && gatewayDataList.size() > 0) {
            for (CsunGatewayData data : gatewayDataList) {
                if (PublicUtil.isEmpty(data.getDeviceNo())) {
                    continue;
                }
                String redisKey = RedisKeyUtil.getCsunGatewayKey(data.getDeviceNo());
                CsunGatewayData csunGatewayData = (CsunGatewayData) redisTemplate.opsForValue().get(redisKey);
                if (PublicUtil.isNotEmpty(csunGatewayData)) {
                    continue;
                }
                redisTemplate.opsForValue().set(redisKey, data);
            }
        }
    }

}
